/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.retrieval.main;

import co.edu.unal.bioingenium.kbmed.features.CollectionFeatures;
import co.edu.unal.bioingenium.kbmed.features.io.FeaturesIO;
import co.edu.unal.bioingenium.kbmed.retrieval.knowledge.process.PrepareByConcepts;
import co.edu.unal.bioingenium.kbmed.text.index.structures.InvertedIndexIntToInt;
import co.edu.unal.bioingenium.kbmed.text.index.io.InvertedIndexIO;
import co.edu.unal.bioingenium.kbmed.text.index.structures.InvertedIndexStrToInt;
import co.edu.unal.bioingenium.kbmed.text.representation.vo.Corpus;
import co.edu.unal.bioingenium.kbmed.text.representation.io.CorpusIO;
import co.edu.unal.bioingenium.kbmed.retrieval.text.process.PrepareByWords;
import java.io.IOException;

/**
 *
 * @author Alejandro Riveros Cruz Riveros Cruz
 */
public class PrepareCollectionMain {

    /**
     * 
     * @param corpusName
     * @param corpusPath
     * @param pruneBellow
     * @param pruneAbove
     * @throws IOException
     */
    public void preProcessByWords(String corpusName, String corpusPath, int pruneBellow, int pruneAbove) throws IOException {
        PrepareByWords prepareCollection = new PrepareByWords();
        //Build and store the corpus
        System.out.print("Building word corpus...");
        Corpus wordCorpus = prepareCollection.buildCorpus(corpusName, corpusPath, pruneBellow, pruneAbove);
        CorpusIO.storeCorpus(wordCorpus, corpusName + "WordsCorpus.dat");
        //Extract and store the collections features
        System.out.print("Extract word features ...");
        CollectionFeatures wordsFeatures = prepareCollection.extractFeatures(wordCorpus);
        FeaturesIO.storeCollectionFeatures(wordsFeatures, corpusName + "WordsFeatures.dat");
        System.out.print("OK\n");
        //Build and store the inverted index
        InvertedIndexIntToInt wordByDocument = prepareCollection.buildWordByDocumentIndex(wordCorpus);
        InvertedIndexIO.storeIntToIntInvertedIndex(wordByDocument, corpusName + "WordsByDocument.idx");
        InvertedIndexStrToInt wordByNameIndex = prepareCollection.buildWordByDocumentNameIndex(wordCorpus);
        InvertedIndexIO.storeStrToIntInvertedIndex(wordByNameIndex, corpusName + "WordsByName.idx");
        InvertedIndexStrToInt metaByDocument = prepareCollection.buildParentDirectoryByDocumentIndex(wordCorpus);
        InvertedIndexIO.storeStrToIntInvertedIndex(metaByDocument, corpusName + "MetaByDocument.idx");
    }

    /**
     * 
     * @param corpusName
     * @param corpusPath
     * @param pruneBellow
     * @param pruneAbove
     * @throws IOException
     */
    public void preProcessByConcepts(String corpusName, String corpusPath, int pruneBellow, int pruneAbove) throws IOException {
        PrepareByConcepts prepareCollection = new PrepareByConcepts();
        //Build and store the corpus
        System.out.print("Building concept corpus...");
        prepareCollection.initKnowledgeSource();
        Corpus conceptCorpus = prepareCollection.buildConceptCorpus(corpusName, corpusPath, pruneBellow, pruneAbove);
        CorpusIO.storeCorpus(conceptCorpus, corpusName + "ConceptsCorpus.dat");
        //Extract and store the collections features
        System.out.print("Extract concept features ...");
        CollectionFeatures conceptsFeatures = prepareCollection.extractFeatures(conceptCorpus);
        FeaturesIO.storeCollectionFeatures(conceptsFeatures, corpusName + "ConceptsFeatures.dat");
        System.out.print("OK\n");
        //Build and store the inverted index
        InvertedIndexIntToInt conceptByDocument = prepareCollection.buildConceptByDocumentIndex(conceptCorpus);
        InvertedIndexIO.storeIntToIntInvertedIndex(conceptByDocument, corpusName + "ConceptsByDocument.idx");
    }

    /**
     * 
     * @param args
     * @throws IOException
     */
    public static void main(String... args) throws IOException {
        args = new String[4];
        args[0] = "saruro";
        args[1] = "/media/ldatos/sartxt/";
        args[2] = "0";
        args[3] = "0";
        PrepareCollectionMain prepareCollectionMain = new PrepareCollectionMain();
        if (args.length < 2) {
            System.out.println("Usage:");
            System.out.println("PrepareCollectionMain corpusName corpusPath [pruneBellow pruneAbove]");
            System.out.println("[pruneBellow pruneAbove]Optional limit to prune words bellow and above that number of occurrences in the corpus");
            System.exit(1);
        } else {
            if (args.length == 2) {
                prepareCollectionMain.preProcessByWords(args[0], args[1], 0, 0);
                prepareCollectionMain.preProcessByConcepts(args[0], args[1], 0, 0);
            } else if (args.length == 4) {
                prepareCollectionMain.preProcessByWords(args[0], args[1], Integer.parseInt(args[2]), Integer.parseInt(args[3]));
                prepareCollectionMain.preProcessByConcepts(args[0], args[1], Integer.parseInt(args[2]), Integer.parseInt(args[3]));
            }
        }

    }
}
